<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 10/24/12
 * Time: 8:19 AM
 * To change this template use File | Settings | File Templates.
 */
class Libs_Bootstrap
{
    public  function __construct()
    {
        $this->delegate();
    }
    public function parseUrl(&$controller,&$action=null,&$param=null)
    {
        if(isset($_GET['url']))
        {
            $arr_url=explode('/',rtrim($_GET['url'],'/'));
            if(file_exists('controllers/'.$arr_url[0].'.php'))
            {
                $controller=$arr_url[0];
                array_shift($arr_url);

            }
            else
            {
                $controller='Index';
                $action='index';
            }
            if(isset($arr_url[0]))
            {
                $class='Controllers_'.$controller;
                if(is_callable(array($class,$arr_url[0])))
                {
                    $action=$arr_url[0];
                    array_shift($arr_url);
                }
                else
                {
                    $action='index';

                }
                if(count($arr_url)>0)
                {
                    $param=$arr_url;
                }
            }
            else
            {
                $action='index';
            }
        }
        else
        {
            $controller='index';
            $action='index';
        }
    }
    public function delegate()
    {
        if($_SESSION['username']!='' and $_SESSION['grade']=='admin'|$_SESSION['grade']=='employee')
        {
            $this->parseUrl($controller,$action,$param);
            $class="Controllers_".$controller;
            $controller=new $class();
            $controller->$action($param,$action);
        }
        else

        {
            $controller=new Controllers_Index();
            $controller->login();
        }

    }

}
